//
//  SSLoginController.m
//  textDemo
//
//  Created by Esther on 2017/2/19.
//  Copyright © 2017年 Esther. All rights reserved.
//

#import "SSLoginController.h"
#import "SSLoginInPutView.h"
#import "SSThirdLoginView.h"
#import "UIView+Toast.h"
#import <UMSocialCore/UMSocialCore.h>

@interface SSLoginController ()
@property(nonatomic, strong) SSLoginInPutView *loginInPutView;
@property(nonatomic, strong) SSThirdLoginView *thirdLoginView;
@end

@implementation SSLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登录";
    [self.view addSubview:self.loginInPutView];
    [self.view addSubview:self.thirdLoginView];
    
    #pragma mark - 约束
    __weak typeof (self) weakSelf = self;
    [_loginInPutView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.equalTo(weakSelf.view);
        make.height.mas_equalTo(180);
        
    }];
    [_thirdLoginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.loginInPutView.mas_bottom);
        make.left.bottom.right.equalTo(weakSelf.view);
    }];
    
}


- (void)loginViewMethod:(NSDictionary *)dict{
//    self.loginBlock(@{@"LoginName" : _userNameTextField.text,
//                      @"Lpassword" : _passwordTextField.text
    NSDictionary *parameters = @{};
    
    [[SSNetworkTools shared] ss_getLoadData:@"appMember/appLogin.do" parameters:parameters completeBlock:^(id responseObject, NSError *error) {
        if (error != nil) {
            return;
        }
        if ([responseObject[@"ErrorMessage"] isEqualToString:@"登陆成功"]) {
            // 偏好设置
            [[NSUserDefaults standardUserDefaults] setObject:responseObject forKey:@"ISLOGIN"];
            
            [self.view makeToast:@"登陆成功" duration:1.5 position:@"center"];
            [self performSelector:@selector(popToRootController) withObject:nil afterDelay:1.5];
            
            
        }else if([responseObject[@"ErrorMessage"] isEqualToString:@"密码错误"]){
            
            [self.view makeToast:@"密码错误" duration:1.5 position:@"center"];
            
        }else if([responseObject[@"ErrorMessage"] isEqualToString:@"用户不存在"]){
            
            [self.view makeToast:@"用户不存在" duration:1.5 position:@"center"];
            
        }
        
    }];
    
    
}
- (void)popToRootController{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark - 懒加载
- (SSLoginInPutView *)loginInPutView{
    if (!_loginInPutView) {
        _loginInPutView = [[SSLoginInPutView alloc] init];
        __weak typeof (self) weakSelf = self;
        _loginInPutView.loginBlock = ^(NSDictionary *dict){
            [weakSelf loginViewMethod:dict];
        };
        
    }
    return _loginInPutView;
}

- (SSThirdLoginView *)thirdLoginView{
    if (!_thirdLoginView) {
        _thirdLoginView = [[SSThirdLoginView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        
        __weak typeof (self) weakSelf = self;
        _thirdLoginView.qqLoginBlock = ^(){
            [weakSelf getAuthWithUserInfoFromWechat];
        };
        
    }
    return _thirdLoginView;
}

// 新浪
- (void)getAuthWithUserInfoFromSina
{
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_Sina currentViewController:nil completion:^(id result, NSError *error) {
        if (error) {
            
        } else {
            UMSocialUserInfoResponse *resp = result;
            
            // 授权信息
            NSLog(@"Sina uid: %@", resp.uid);
            NSLog(@"Sina accessToken: %@", resp.accessToken);
            NSLog(@"Sina refreshToken: %@", resp.refreshToken);
            NSLog(@"Sina expiration: %@", resp.expiration);
            
            // 用户信息
            NSLog(@"Sina name: %@", resp.name);
            NSLog(@"Sina iconurl: %@", resp.iconurl);
            NSLog(@"Sina gender: %@", resp.gender);
            
            // 第三方平台SDK源数据
            NSLog(@"Sina originalResponse: %@", resp.originalResponse);
        }
    }];
}

// qq
- (void)getAuthWithUserInfoFromQQ
{
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_QQ currentViewController:nil completion:^(id result, NSError *error) {
        if (error) {
            
        } else {
            UMSocialUserInfoResponse *resp = result;
            
            // 授权信息
            NSLog(@"QQ uid: %@", resp.uid);
            NSLog(@"QQ openid: %@", resp.openid);
            NSLog(@"QQ accessToken: %@", resp.accessToken);
            NSLog(@"QQ expiration: %@", resp.expiration);
            
            // 用户信息
            NSLog(@"QQ name: %@", resp.name);
            NSLog(@"QQ iconurl: %@", resp.iconurl);
            NSLog(@"QQ gender: %@", resp.gender);
            
            // 第三方平台SDK源数据
            NSLog(@"QQ originalResponse: %@", resp.originalResponse);
        }
    }];
}

// 微信
- (void)getAuthWithUserInfoFromWechat
{
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_WechatSession currentViewController:nil completion:^(id result, NSError *error) {
        if (error) {
            
        } else {
            UMSocialUserInfoResponse *resp = result;
            
            //登录成功后Push近手机号注册页面
            
            // 授权信息
            NSLog(@"Wechat uid: %@", resp.uid);
            NSLog(@"Wechat openid: %@", resp.openid);
            NSLog(@"Wechat accessToken: %@", resp.accessToken);
            NSLog(@"Wechat refreshToken: %@", resp.refreshToken);
            NSLog(@"Wechat expiration: %@", resp.expiration);
            
            // 用户信息
            NSLog(@"Wechat name: %@", resp.name);
            NSLog(@"Wechat iconurl: %@", resp.iconurl);
            NSLog(@"Wechat gender: %@", resp.gender);
            
            // 第三方平台SDK源数据
            NSLog(@"Wechat originalResponse: %@", resp.originalResponse);
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
